﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace tsgl
{
    public partial class bookReturnOrRenew : Form
    {
        public bookReturnOrRenew()
        {
            InitializeComponent();
        }

        string flagBorrow,borrowerId, bookBarcode, bibliothecaId;

        private void getBorrowerLoanInfo()
        {
            if (loanClass.isOverdue(borrowerId) == true)
            {
                DataTable dt = loanClass.getLoanInfo(borrowerId);
                if (dt.Rows.Count != 0)
                {
                    this.dataGridView1.DataSource = dt;
                    this.btnSelectBookCode.Enabled = false;
                }
                else
                {

                }
            }
            else
            {
                MessageBox.Show("你已有超期的图书了，请归还再借！","提示信息");
                //
                //
                //
            }
        }

        private void getBook()
        {
            DataTable dt = loanClass.getBookInfo(bookBarcode);
            if (dt.Rows.Count != 0)
            {
                this.txtBookName.Text = dt.Rows[0]["图书名称"].ToString();
                this.txtPublisherName.Text=dt.Rows[0]["出版社名称"].ToString();
                this.txtAuthor.Text=dt.Rows[0]["作者"].ToString();
                bibliothecaId=dt.Rows[0]["书目编号"].ToString();
            }
        }

        private void clearControl()
        {
            this.txtBookName.Text = "";
            this.txtBookCode.Text = "";
            this.txtPublisherName.Text = "";
            this.txtAuthor.Text = "";
        }

        private void bookReturnOrRenew_Load(object sender, EventArgs e)
        {
            if (flagBorrow == "renew")
            {
                this.Text = "续借图书";
                this.btnxuejie.Enabled = true;
                this.btnguihuan.Enabled = false;
            }
            else
            {
                this.Text = "归还图书";
                this.btnxuejie.Enabled = false;
                this.btnguihuan.Enabled = true;
            }
            this.txtBookName.Enabled = false;
            this.txtPublisherName.Enabled = false;
            this.txtAuthor.Enabled = false;
            this.txtBorrowerId.Focus();
        }

        private void btnSelectBorrowerId_Click(object sender, EventArgs e)
        {
            selectBorrower frmBorrowerSelect = new selectBorrower();
            frmBorrowerSelect.ShowDialog();
            borrowerId = frmBorrowerSelect.getBorrowerId();
            this.txtBorrowerId.Text = borrowerId;
            getBorrowerLoanInfo();
        }

        private void txtBorrowerId_KeyDown(object sender, KeyEventArgs e)
        {
            if(borrowerId.Trim().Length>=5)
            {
                borrowerId=txtBorrowerId.Text.Trim();
                getBorrowerLoanInfo();
            }
        }

        private void btnSelectBookCode_Click(object sender, EventArgs e)
        {
            DataTable dt;
            selectBook frmBookSelect = new selectBook();
            frmBookSelect.ShowDialog();
            bookBarcode = frmBookSelect.getBookCode();
            this.txtBookCode.Text = bookBarcode;
            getBook();
            dt = returnOrRenewClass.getLoanInfoByBarcode(bookBarcode);
            //this.txtBorrowerId.Text=dt.Rows[0]
            this.dataGridView1.DataSource = dt;
            this.btnSelectBorrowerId.Enabled = false;
            this.txtBorrowerId.Enabled = false;
        }

        private void txtBookCode_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtBookCode_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode = Keys.Enter)
            //{
            //    bookBarcode = txtBookCode.Text.Trim();
            //    getBook();
            //    if(this.btnSelectBookCode.Enabled==true)
            //    {
            //        DataTable dt = returnOrRenewClass.getLoanInfoByBarcode(bookBarcode);
            //        this.txtBorrowerId.Text=dt.Rows[0][0].ToString();
            //    }
            //}
        }

        private void btnxuejie_Click(object sender, EventArgs e)
        {
            string strCardState;
            int maxDay;
            DataTable dt;
            if (this.txtBorrowerId.Text == "")
            {
                MessageBox.Show("借阅者编号不能为空！","提示信息");
            }
            if (this.txtBookCode.Text == "")
            {
                MessageBox.Show("图书条码不能为空！","提示信息");
            }
            dt = loanClass.getBorrowerInfo(borrowerId);
            maxDay = int.Parse(dt.Rows[0]["最长借书期限"].ToString());
            strCardState = dt.Rows[0]["借书证状态"].ToString();
            switch (strCardState)
            {
                case "有效":
                    if (returnOrRenewClass.loanRenew(maxDay, borrowerId, bookBarcode) == true)
                    {
                        this.dataGridView1.DataSource = loanClass.getLoanInfo(borrowerId);
                        MessageBox.Show("图书续借成功！", "提示信息");
                    }
                    else
                    {
                        MessageBox.Show("图书续借失败！", "提示信息",MessageBoxButtons.OK,MessageBoxIcon.Question);
                    }
                    break;
                case "挂失":
                    MessageBox.Show("读者的借书证已被挂失！", "提示信息");
                    break;
                case "停用":
                    MessageBox.Show("读者的借书证已停用！", "提示信息");
                    break;
            }
            btnSelectBookCode.Enabled = false;
        }

        private void btnguihuan_Click(object sender, EventArgs e)
        {
            if (returnOrRenewClass.loanDelete(borrowerId, bookBarcode) == true)
            {
                loanClass.setBookState(bookBarcode, "在藏");
                string str = "update 书目信息 set 现存数量=现存数量+1 where 书目编号=@a";
                bookDbClass.ExecuteNonQurey(str, new SqlParameter("a", bibliothecaId));
                this.dataGridView1.DataSource = loanClass.getLoanInfo(borrowerId);
                MessageBox.Show("图书归还成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("图书归还失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}